﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using FlexCell;
using YArchitech.Controls;
using libxl;
using Transfer.Utils;

namespace HyCommonTableTool
{
	public class GridToolExportImportExcel
	{
		public GridToolExportImportExcel(bool bXlsx)
		{
			if (bXlsx)
			{
				this.m_book = this.m_xmlBook;
			}
			else
			{
				this.m_book = this.m_binBook;
			}
			this.m_book.setKey("Beijing YArchitech Tongxing Technology Co.,Ltd", "windows-282b2c0f08cbe70966b4616ea0i8d7n0");
			this.GetXYDpi(ref this.m_dpiX, ref this.m_dpiY);
		}

		private void GetXYDpi(ref double Xdpi, ref double Ydpi)
		{
			using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
			{
				Xdpi = (double)graphics.DpiX;
				Ydpi = (double)graphics.DpiY;
			}
		}

		public bool ExportExcel(Grid grid, string FilePath)
		{
			if (grid == null || string.IsNullOrEmpty(FilePath))
			{
				return false;
			}
			if (this.DataFromGridToExcel(grid))
			{
				this.m_book.save(FilePath);
				Process.Start(FilePath);
				return true;
			}
			return false;
		}

		private bool DataFromGridToExcel(Grid grid)
		{
			if (grid == null || this.m_book == null)
			{
				return false;
			}
			Sheet sheet = this.m_book.addSheet("sheet1");
			int rows = grid.Rows;
			int cols = grid.Cols;
			List<Range> list = new List<Range>();
			for (int i = 1; i < cols; i++)
			{
				sheet.setCol(i - 1, this.UnitTransform(grid.Column(i).Width, false));
			}
			for (int j = 1; j < rows; j++)
			{
				sheet.setRow(j - 1, this.UnitTransform(grid.Row(j).Height, true));
				int k = 1;
				while (k < cols)
				{
					Cell cell = grid.Cell(j, k);
					if (!cell.MergeCell)
					{
						goto IL_F3;
					}
					Range range = grid.Range(j, k, j, k);
					if (!this.IfRangeExist(list, range))
					{
						list.Add(range);
						sheet.setMerge(range.FirstRow - 1, range.LastRow - 1, range.FirstCol - 1, range.LastCol - 1);
						goto IL_F3;
					}
					IL_384:
					k++;
					continue;
					IL_F3:
					string text = cell.ImageKey;
					if (text != "")
					{
						FlexCell.Image image = grid.Images.Item(text);
						if (string.IsNullOrEmpty(Path.GetExtension(text)))
						{
							text += ".jpg";
						}
						string text2 = Environment.GetEnvironmentVariable("TEMP");
						text2 = text2 + "\\" + text;
						image.SaveAs(text2, this.GetImageFormat(text));
						int pictureId = this.m_book.addPicture(text2);
						int width = image.Picture.Width;
						int height = image.Picture.Height;
						this.GetImageSize(image, cell.Alignment, grid.Column(k).Width, grid.Row(j).Height, ref width, ref height);
						sheet.setPicture(j - 1, k - 1, pictureId, width, height);
						File.Delete(text2);
						goto IL_384;
					}
					Format format = this.m_book.addFormat();
					if (cell.Text != "")
					{
						libxl.Font font = this.m_book.addFont();
						font.name = cell.Font.Name;
						font.size = Convert.ToInt32(cell.Font.Size);
						font.color = this.TransformColor(cell.ForeColor);
						font.bold = cell.Font.Bold;
						font.italic = cell.Font.Italic;
						font.strikeOut = cell.Font.Strikeout;
						font.underline = (cell.Font.Underline ? Underline.UNDERLINE_SINGLE : Underline.UNDERLINE_NONE);
						format.font = font;
						if (cell.WrapText)
						{
							format.wrap = true;
						}
					}
					libxl.Color color = this.TransformColor(cell.BackColor);
					if (color != libxl.Color.COLOR_WHITE)
					{
						format.patternForegroundColor = color;
						format.fillPattern = FillPattern.FILLPATTERN_SOLID;
					}
					this.SetAlignment(ref format, cell.Alignment);
					format.rotation = ((cell.Orientation == TextOrientationEnum.Horizontal) ? 0 : 90);
					this.SetBorderStyle(ref format, cell);
					sheet.writeStr(j - 1, k - 1, cell.Text, format);
					if (cell.MergeCell)
					{
						Range range2 = grid.Range(j, k, j, k);
						for (int l = range2.FirstRow; l <= range2.LastRow; l++)
						{
							for (int m = range2.FirstCol; m <= range2.LastCol; m++)
							{
								sheet.setCellFormat(l - 1, m - 1, format);
							}
						}
						goto IL_384;
					}
					goto IL_384;
				}
			}
			return true;
		}

		public bool ImportExcel(ref Grid grid, string FilePath)
		{
			if (grid == null || string.IsNullOrEmpty(FilePath))
			{
				return false;
			}
			if (!File.Exists(FilePath))
			{
				return false;
			}
			try
			{
				this.m_book.load(FilePath);
			}
			catch
			{
				YJKMessageBox.Show("未能正常打开文档，请检查后重新尝试");
				return false;
			}
			return this.DataFromExcelToGrid(ref grid);
		}

		private bool DataFromExcelToGrid(ref Grid grid)
		{
			if (this.m_book == null || grid == null)
			{
				return false;
			}
			Sheet sheet = this.m_book.getSheet(0);
			this.CreateNewGrid(sheet, ref grid);
			grid.AutoRedraw = false;
			for (int i = sheet.firstCol(); i < sheet.lastCol(); i++)
			{
				grid.Column(i + 1).Width = this.UnitTransform(sheet.colWidth(i), false);
			}
			int num = 0;
			int num2 = 0;
			int num3 = 0;
			int num4 = 0;
			List<Rangex> list = new List<Rangex>();
			for (int j = sheet.firstRow(); j < sheet.lastRow(); j++)
			{
				grid.Row(j + 1).Height = this.UnitTransform(sheet.rowHeight(j), true);
				int k = sheet.firstCol();
				while (k < sheet.lastCol())
				{
					bool mergeX = sheet.getMergeX(j, k, ref num, ref num2, ref num3, ref num4);
					if (!mergeX)
					{
						goto IL_102;
					}
					Rangex rangex = new Rangex(num, num2, num3, num4);
					if (!this.IfRangeExist(list, rangex))
					{
						list.Add(rangex);
						grid.Range(num + 1, num3 + 1, num2 + 1, num4 + 1).Merge();
						goto IL_102;
					}
					IL_298:
					k++;
					continue;
					IL_102:
					Format format = sheet.cellFormat(j, k);
					string text = "";
					CellType cellType = sheet.cellType(j, k);
					if (cellType == CellType.CELLTYPE_BLANK || cellType == CellType.CELLTYPE_ERROR || cellType == CellType.CELLTYPE_EMPTY)
					{
						goto IL_298;
					}
					if (cellType == CellType.CELLTYPE_NUMBER)
					{
						text = sheet.readNum(j, k).ToString();
					}
					else if (cellType == CellType.CELLTYPE_STRING)
					{
						text = sheet.readStr(j, k);
					}
					else if (cellType == CellType.CELLTYPE_BOOLEAN)
					{
						text = sheet.readBool(j, k).ToString();
					}
					Cell cell = grid.Cell(j + 1, k + 1);
					cell.Text = text;
					cell.FontName = format.font.name;
					cell.FontSize = Convert.ToSingle(format.font.size);
					cell.ForeColor = this.TransformColor(format.font.color);
					cell.FontBold = format.font.bold;
					cell.FontItalic = format.font.italic;
					cell.FontStrikeout = format.font.strikeOut;
					cell.FontUnderline = (format.font.underline != Underline.UNDERLINE_NONE);
					cell.WrapText = format.wrap;
					cell.BackColor = this.TransformColor(format.patternForegroundColor);
					this.SetAlignment(format, ref cell);
					cell.Orientation = ((format.rotation == 0) ? TextOrientationEnum.Horizontal : TextOrientationEnum.Vertical);
					if (mergeX)
					{
						Format endFormat = sheet.cellFormat(num2, num4);
						this.SetBorderStyle(format, endFormat, ref cell);
						goto IL_298;
					}
					this.SetBorderStyle(format, ref cell);
					goto IL_298;
				}
			}
			for (int l = 0; l < sheet.pictureSize(); l++)
			{
				int num5 = 0;
				int num6 = 0;
				int num7 = 0;
				int num8 = 0;
				sheet.getPicture(l, ref num5, ref num6, ref num7, ref num8);
				if (num7 > grid.Rows - 1)
				{
					grid.Rows = num7 + 2;
					for (int m = sheet.lastRow() + 1; m < num7 + 2; m++)
					{
						grid.Row(m).Height = this.UnitTransform(sheet.rowHeight(m), true);
					}
				}
				if (num8 > grid.Cols - 1)
				{
					grid.Cols = num8 + 2;
					for (int n = sheet.lastCol() + 1; n < num8 + 2; n++)
					{
						grid.Column(n).Width = this.UnitTransform(sheet.colWidth(n), false);
					}
				}
				byte[] array = null;
				int num9 = 0;
				PictureType picture = this.m_book.getPicture(l, ref array, ref num9);
				if (array != null)
				{
					string text2 = this.SetImageFormat(picture);
					string text3 = l.ToString() + text2;
					string text4 = Environment.GetEnvironmentVariable("TEMP") + "\\" + text3;
					System.Drawing.Image image = System.Drawing.Image.FromStream(new MemoryStream(array));
					image.Save(text4, this.GetImageFormat(text2));
					grid.Images.Add(text4, text3);
					grid.Cell(num5 + 1, num6 + 1).SetImage(text3);
					grid.Range(num5 + 1, num6 + 1, num7 + 1, num8 + 1).Merge();
					if (image != null)
					{
						image.Dispose();
					}
					File.Delete(text4);
				}
			}
			grid.AutoRedraw = true;
			grid.Refresh();
			return true;
		}

		private ImageFormat GetImageFormat(string strImgKey)
		{
			string extension = Path.GetExtension(strImgKey);
			if (extension.Equals(".jpg", StringComparison.OrdinalIgnoreCase) || extension.Equals(".jpeg", StringComparison.OrdinalIgnoreCase))
			{
				return ImageFormat.Jpeg;
			}
			if (extension.Equals(".png", StringComparison.OrdinalIgnoreCase))
			{
				return ImageFormat.Png;
			}
			if (extension.Equals(".gif", StringComparison.OrdinalIgnoreCase))
			{
				return ImageFormat.Gif;
			}
			if (extension.Equals(".icon", StringComparison.OrdinalIgnoreCase) || extension.Equals(".ico", StringComparison.OrdinalIgnoreCase))
			{
				return ImageFormat.Icon;
			}
			if (extension.Equals(".tif", StringComparison.OrdinalIgnoreCase) || extension.Equals(".tiff", StringComparison.OrdinalIgnoreCase))
			{
				return ImageFormat.Tiff;
			}
			if (extension.Equals(".bmp", StringComparison.OrdinalIgnoreCase))
			{
				return ImageFormat.Bmp;
			}
			return ImageFormat.Jpeg;
		}

		private string SetImageFormat(PictureType picType)
		{
			string result;
			if (picType != PictureType.PICTURETYPE_PNG)
			{
				if (picType != PictureType.PICTURETYPE_JPEG)
				{
					if (picType != PictureType.PICTURETYPE_TIFF)
					{
						result = ".jpg";
					}
					else
					{
						result = ".tif";
					}
				}
				else
				{
					result = ".jpg";
				}
			}
			else
			{
				result = ".png";
			}
			return result;
		}

		private double UnitTransform(short nPixel, bool bRow)
		{
			double num = Convert.ToDouble(nPixel);
			if (bRow)
			{
				num = num / this.m_dpiY * 2.54 * 28.6;
			}
			else
			{
				num = (num - 12.0) / 7.0 + 1.0;
			}
			return num;
		}

		private short UnitTransform(double dValue, bool bRow)
		{
			short result;
			if (bRow)
			{
				result = Convert.ToInt16(dValue / 28.6 / 2.54 * this.m_dpiY);
			}
			else
			{
				result = Convert.ToInt16((dValue - 1.0) * 7.0 + 12.0);
			}
			return result;
		}

		private libxl.Color TransformColor(System.Drawing.Color color)
		{
			return this.m_binBook.colorPack((int)color.R, (int)color.G, (int)color.B);
		}

		private System.Drawing.Color TransformColor(libxl.Color color)
		{
			if (color == libxl.Color.COLOR_DEFAULT_FOREGROUND)
			{
				return System.Drawing.Color.White;
			}
			if (color == (libxl.Color)(-1))
			{
				return System.Drawing.Color.Black;
			}
			int red = 0;
			int green = 0;
			int blue = 0;
			this.m_binBook.colorUnpack(color, ref red, ref green, ref blue);
			return System.Drawing.Color.FromArgb(red, green, blue);
		}

		private void SetAlignment(ref Format format, AlignmentEnum alignment)
		{
			if (format == null)
			{
				return;
			}
			switch (alignment)
			{
			case AlignmentEnum.LeftTop:
				format.alignH = AlignH.ALIGNH_LEFT;
				format.alignV = AlignV.ALIGNV_TOP;
				return;
			case AlignmentEnum.LeftCenter:
				format.alignH = AlignH.ALIGNH_LEFT;
				format.alignV = AlignV.ALIGNV_CENTER;
				return;
			case AlignmentEnum.LeftBottom:
				format.alignH = AlignH.ALIGNH_LEFT;
				format.alignV = AlignV.ALIGNV_BOTTOM;
				return;
			case AlignmentEnum.CenterTop:
				format.alignH = AlignH.ALIGNH_CENTER;
				format.alignV = AlignV.ALIGNV_TOP;
				return;
			case AlignmentEnum.CenterCenter:
				format.alignH = AlignH.ALIGNH_CENTER;
				format.alignV = AlignV.ALIGNV_CENTER;
				return;
			case AlignmentEnum.CenterBottom:
				format.alignH = AlignH.ALIGNH_CENTER;
				format.alignV = AlignV.ALIGNV_BOTTOM;
				return;
			case AlignmentEnum.RightTop:
				format.alignH = AlignH.ALIGNH_RIGHT;
				format.alignV = AlignV.ALIGNV_TOP;
				return;
			case AlignmentEnum.RightCenter:
				format.alignH = AlignH.ALIGNH_RIGHT;
				format.alignV = AlignV.ALIGNV_CENTER;
				return;
			case AlignmentEnum.RightBottom:
				format.alignH = AlignH.ALIGNH_RIGHT;
				format.alignV = AlignV.ALIGNV_BOTTOM;
				return;
			}
			format.alignH = AlignH.ALIGNH_CENTER;
			format.alignV = AlignV.ALIGNV_CENTER;
		}

		private void SetAlignment(Format format, ref Cell cell)
		{
			if (format == null || cell == null)
			{
				return;
			}
			AlignH alignH = format.alignH;
			AlignV alignV = format.alignV;
			if (format.alignH == AlignH.ALIGNH_LEFT && format.alignV == AlignV.ALIGNV_TOP)
			{
				cell.Alignment = AlignmentEnum.LeftTop;
				return;
			}
			if (format.alignH == AlignH.ALIGNH_LEFT && format.alignV == AlignV.ALIGNV_CENTER)
			{
				cell.Alignment = AlignmentEnum.LeftCenter;
				return;
			}
			if (format.alignH == AlignH.ALIGNH_LEFT && format.alignV == AlignV.ALIGNV_BOTTOM)
			{
				cell.Alignment = AlignmentEnum.LeftBottom;
				return;
			}
			if (format.alignH == AlignH.ALIGNH_CENTER && format.alignV == AlignV.ALIGNV_TOP)
			{
				cell.Alignment = AlignmentEnum.CenterTop;
				return;
			}
			if (format.alignH == AlignH.ALIGNH_CENTER && format.alignV == AlignV.ALIGNV_CENTER)
			{
				cell.Alignment = AlignmentEnum.CenterCenter;
				return;
			}
			if (format.alignH == AlignH.ALIGNH_CENTER && format.alignV == AlignV.ALIGNV_BOTTOM)
			{
				cell.Alignment = AlignmentEnum.CenterBottom;
				return;
			}
			if (format.alignH == AlignH.ALIGNH_RIGHT && format.alignV == AlignV.ALIGNV_TOP)
			{
				cell.Alignment = AlignmentEnum.RightTop;
				return;
			}
			if (format.alignH == AlignH.ALIGNH_RIGHT && format.alignV == AlignV.ALIGNV_CENTER)
			{
				cell.Alignment = AlignmentEnum.RightCenter;
				return;
			}
			if (format.alignH == AlignH.ALIGNH_RIGHT && format.alignV == AlignV.ALIGNV_BOTTOM)
			{
				cell.Alignment = AlignmentEnum.RightBottom;
				return;
			}
			cell.Alignment = AlignmentEnum.CenterCenter;
		}

		private void SetBorderStyle(ref Format format, Cell cell)
		{
			if (format == null || cell == null)
			{
				return;
			}
			LineStyleEnum lineStyleEnum = cell.get_Border(EdgeEnum.Left);
			if (lineStyleEnum == LineStyleEnum.Thick)
			{
				format.borderLeft = BorderStyle.BORDERSTYLE_THICK;
			}
			else if (lineStyleEnum == LineStyleEnum.Thin)
			{
				format.borderLeft = BorderStyle.BORDERSTYLE_THIN;
			}
			lineStyleEnum = cell.get_Border(EdgeEnum.Right);
			if (lineStyleEnum == LineStyleEnum.Thick)
			{
				format.borderRight = BorderStyle.BORDERSTYLE_THICK;
			}
			else if (lineStyleEnum == LineStyleEnum.Thin)
			{
				format.borderRight = BorderStyle.BORDERSTYLE_THIN;
			}
			lineStyleEnum = cell.get_Border(EdgeEnum.Top);
			if (lineStyleEnum == LineStyleEnum.Thick)
			{
				format.borderTop = BorderStyle.BORDERSTYLE_THICK;
			}
			else if (lineStyleEnum == LineStyleEnum.Thin)
			{
				format.borderTop = BorderStyle.BORDERSTYLE_THIN;
			}
			lineStyleEnum = cell.get_Border(EdgeEnum.Bottom);
			if (lineStyleEnum == LineStyleEnum.Thick)
			{
				format.borderBottom = BorderStyle.BORDERSTYLE_THICK;
			}
			else if (lineStyleEnum == LineStyleEnum.Thin)
			{
				format.borderBottom = BorderStyle.BORDERSTYLE_THIN;
			}
			if (cell.get_Border(EdgeEnum.DiagonalUp) != LineStyleEnum.None && cell.get_Border(EdgeEnum.DiagonalDown) == LineStyleEnum.None)
			{
				format.borderStyle = BorderStyle.BORDERSTYLE_THIN;
				format.borderDiagonal = BorderDiagonal.BORDERDIAGONAL_UP;
			}
			else if (cell.get_Border(EdgeEnum.DiagonalUp) == LineStyleEnum.None && cell.get_Border(EdgeEnum.DiagonalDown) != LineStyleEnum.None)
			{
				format.borderStyle = BorderStyle.BORDERSTYLE_THIN;
				format.borderDiagonal = BorderDiagonal.BORDERDIAGONAL_DOWN;
			}
			if (cell.get_Border(EdgeEnum.DiagonalUp) != LineStyleEnum.None && cell.get_Border(EdgeEnum.DiagonalDown) != LineStyleEnum.None)
			{
				format.borderStyle = BorderStyle.BORDERSTYLE_THIN;
				format.borderDiagonal = BorderDiagonal.BORDERDIAGONAL_BOTH;
			}
		}

		private void SetBorderStyle(Format format, ref Cell cell)
		{
			if (format == null || cell == null)
			{
				return;
			}
			BorderStyle borderStyle = format.borderLeft;
			if (borderStyle == BorderStyle.BORDERSTYLE_THICK || borderStyle == BorderStyle.BORDERSTYLE_MEDIUM)
			{
				cell.set_Border(EdgeEnum.Left, LineStyleEnum.Thick);
			}
			else if (borderStyle == BorderStyle.BORDERSTYLE_THIN)
			{
				cell.set_Border(EdgeEnum.Left, LineStyleEnum.Thin);
			}
			borderStyle = format.borderRight;
			if (borderStyle == BorderStyle.BORDERSTYLE_THICK || borderStyle == BorderStyle.BORDERSTYLE_MEDIUM)
			{
				cell.set_Border(EdgeEnum.Right, LineStyleEnum.Thick);
			}
			else if (borderStyle == BorderStyle.BORDERSTYLE_THIN)
			{
				cell.set_Border(EdgeEnum.Right, LineStyleEnum.Thin);
			}
			borderStyle = format.borderTop;
			if (borderStyle == BorderStyle.BORDERSTYLE_THICK || borderStyle == BorderStyle.BORDERSTYLE_MEDIUM)
			{
				cell.set_Border(EdgeEnum.Top, LineStyleEnum.Thick);
			}
			else if (borderStyle == BorderStyle.BORDERSTYLE_THIN)
			{
				cell.set_Border(EdgeEnum.Top, LineStyleEnum.Thin);
			}
			borderStyle = format.borderBottom;
			if (borderStyle == BorderStyle.BORDERSTYLE_THICK || borderStyle == BorderStyle.BORDERSTYLE_MEDIUM)
			{
				cell.set_Border(EdgeEnum.Bottom, LineStyleEnum.Thick);
			}
			else if (borderStyle == BorderStyle.BORDERSTYLE_THIN)
			{
				cell.set_Border(EdgeEnum.Bottom, LineStyleEnum.Thin);
			}
			switch (format.borderDiagonal)
			{
			case BorderDiagonal.BORDERDIAGONAL_DOWN:
				cell.set_Border(EdgeEnum.DiagonalDown, LineStyleEnum.Thin);
				return;
			case BorderDiagonal.BORDERDIAGONAL_UP:
				cell.set_Border(EdgeEnum.DiagonalUp, LineStyleEnum.Thin);
				return;
			case BorderDiagonal.BORDERDIAGONAL_BOTH:
				cell.set_Border(EdgeEnum.DiagonalUp, LineStyleEnum.Thin);
				cell.set_Border(EdgeEnum.DiagonalDown, LineStyleEnum.Thin);
				return;
			default:
				return;
			}
		}

		private void SetBorderStyle(Format startFormat, Format endFormat, ref Cell cell)
		{
			if (startFormat == null || endFormat == null || cell == null)
			{
				return;
			}
			BorderStyle borderStyle = startFormat.borderLeft;
			if (borderStyle == BorderStyle.BORDERSTYLE_THICK || borderStyle == BorderStyle.BORDERSTYLE_MEDIUM)
			{
				cell.set_Border(EdgeEnum.Left, LineStyleEnum.Thick);
			}
			else if (borderStyle == BorderStyle.BORDERSTYLE_THIN)
			{
				cell.set_Border(EdgeEnum.Left, LineStyleEnum.Thin);
			}
			borderStyle = startFormat.borderTop;
			if (borderStyle == BorderStyle.BORDERSTYLE_THICK || borderStyle == BorderStyle.BORDERSTYLE_MEDIUM)
			{
				cell.set_Border(EdgeEnum.Top, LineStyleEnum.Thick);
			}
			else if (borderStyle == BorderStyle.BORDERSTYLE_THIN)
			{
				cell.set_Border(EdgeEnum.Top, LineStyleEnum.Thin);
			}
			borderStyle = endFormat.borderRight;
			if (borderStyle == BorderStyle.BORDERSTYLE_THICK || borderStyle == BorderStyle.BORDERSTYLE_MEDIUM)
			{
				cell.set_Border(EdgeEnum.Right, LineStyleEnum.Thick);
			}
			else if (borderStyle == BorderStyle.BORDERSTYLE_THIN)
			{
				cell.set_Border(EdgeEnum.Right, LineStyleEnum.Thin);
			}
			borderStyle = endFormat.borderBottom;
			if (borderStyle == BorderStyle.BORDERSTYLE_THICK || borderStyle == BorderStyle.BORDERSTYLE_MEDIUM)
			{
				cell.set_Border(EdgeEnum.Bottom, LineStyleEnum.Thick);
			}
			else if (borderStyle == BorderStyle.BORDERSTYLE_THIN)
			{
				cell.set_Border(EdgeEnum.Bottom, LineStyleEnum.Thin);
			}
			switch (startFormat.borderDiagonal)
			{
			case BorderDiagonal.BORDERDIAGONAL_DOWN:
				cell.set_Border(EdgeEnum.DiagonalDown, LineStyleEnum.Thin);
				return;
			case BorderDiagonal.BORDERDIAGONAL_UP:
				cell.set_Border(EdgeEnum.DiagonalUp, LineStyleEnum.Thin);
				return;
			case BorderDiagonal.BORDERDIAGONAL_BOTH:
				cell.set_Border(EdgeEnum.DiagonalUp, LineStyleEnum.Thin);
				cell.set_Border(EdgeEnum.DiagonalDown, LineStyleEnum.Thin);
				return;
			default:
				return;
			}
		}

		private void CreateNewGrid(Sheet sheet, ref Grid grid)
		{
			grid.NewFile();
			grid.DisplayRowNumber = true;
			grid.ShowResizeTip = true;
			grid.Rows = sheet.lastRow() + 1;
			grid.Cols = sheet.lastCol() + 1;
			for (int i = 1; i < grid.Cols; i++)
			{
				grid.Cell(0, i).Text = i.ToString();
			}
		}

		private bool IfRangeExist(List<Range> lstRange, Range range)
		{
			foreach (Range range2 in lstRange)
			{
				if (range2.FirstRow == range.FirstRow && range2.FirstCol == range.FirstCol && range2.LastRow == range.LastRow && range2.LastCol == range.LastCol)
				{
					return true;
				}
			}
			return false;
		}

		private bool IfRangeExist(List<Rangex> lstRange, Rangex range)
		{
			foreach (Rangex rangex in lstRange)
			{
				if (rangex.RowFirst == range.RowFirst && rangex.ColFirst == range.ColFirst && rangex.RowLast == range.RowLast && rangex.ColLast == range.ColLast)
				{
					return true;
				}
			}
			return false;
		}

		private void GetImageSize(FlexCell.Image image, AlignmentEnum cellAlign, short cellWidth, short cellHeight, ref int imgWidth, ref int imgHeight)
		{
			if (image == null)
			{
				return;
			}
			if (cellAlign != AlignmentEnum.CenterCenter)
			{
				imgWidth = (int)cellWidth;
				imgHeight = (int)cellHeight;
				return;
			}
			if (Geometry.GreaterThan((double)cellWidth, (double)imgWidth) && Geometry.GreaterThan((double)cellHeight, (double)imgHeight))
			{
				return;
			}
			if (Geometry.GreaterThan((double)cellWidth, (double)cellHeight))
			{
				imgWidth = (int)cellHeight * imgWidth / imgHeight;
				imgHeight = (int)cellHeight;
				return;
			}
			imgHeight = (int)cellWidth * imgHeight / imgWidth;
			imgWidth = (int)cellWidth;
		}

		private Book m_binBook = new BinBook();

		private Book m_xmlBook = new XmlBook();

		private Book m_book;

		private double m_dpiX;

		private double m_dpiY;
	}
}
